<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use kartik\select2\Select2;
use kartik\touchspin\TouchSpin;
use app\modules\Finance\models\Invoice;

/* @var $this yii\web\View */
/* @var $model app\modules\Finance\models\Invoice */
/* @var $form yii\widgets\ActiveForm */

$orders = $model->finance->order;

$query = Invoice::find()->where(['finance_id'=>$model->finance_id]);

if($model->id){
    $query->andWhere(['not in','id',$model->id]); 
}

$invoices = $query->all();
 
$II_pirce = [];
if($model->invoiceOrder){
    foreach($model->invoiceOrder as $inOrder){
        $II_pirce[$inOrder->order_id] = $inOrder->price;
    } 
} 



$price_arr = [];

if($orders){
    foreach($orders as $order){
         $price_arr[$order->order_id] = 0;
         if(!isset($II_pirce[$order->order_id])){
            $II_pirce[$order->order_id] = 0;
         }
    }
}


if($invoices){
    foreach($invoices as $in){
        if($in->invoiceOrder){
            foreach($in->invoiceOrder as $inOrder){
                 $price_arr[$inOrder->order_id] += $inOrder->price;
            }
        }
    }
}

$invoice_name = \yii::$app->params['invoice_name'];
unset($invoice_name[1]);
unset($invoice_name[2]);
unset($invoice_name[3]);
unset($invoice_name[4]);
unset($invoice_name[5]);
unset($invoice_name[6]);
unset($invoice_name[7]);
unset($invoice_name[8]);
unset($invoice_name[9]);


?>
<style type="text/css">
    .bootstrap-touchspin .input-group-btn-vertical .bootstrap-touchspin-up{display: none;}
    .bootstrap-touchspin .input-group-btn-vertical .bootstrap-touchspin-down{display: none;}
</style>

<div class="invoice-form row">

   <?php $form = ActiveForm::begin([
                                 'options' => ['enctype' => 'multipart/form-data',],
                                 'layout' => 'horizontal',
                                 'fieldConfig' => [
                                    'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                                    'horizontalCssClasses' => [
                                        'label' => 'col-sm-3',
                                        'wrapper' => 'col-sm-8',
                                        'error' => '',
                                    ],
                                 ],
    ]); ?>
    <?= $form->field($model, 'type')->label(false)->hiddenInput() ?>
    <?= $form->field($model, 'status')->label(false)->hiddenInput() ?>
    <?= $form->field($model, 'finance_id')->label(false)->hiddenInput() ?>
    <div class="col-md-6">
    <?= $form->field($model, 'name')->widget(Select2::classname(), [
                'data' => $invoice_name,
                'language' => 'zh-cn',
                'options' => ['placeholder' => '选择服务项目'],
                'pluginOptions' => [
                    'allowClear' => true
                ],
            ]) ?>

    <div class="form-group">
        <label class="control-label col-sm-3" for="order-company"><?= Yii::t('app.invoice', 'Company') ?></label>
        <div class="col-sm-8">
        <?= Select2::widget([
                'name'  => 'Invoice[company]',
                'id'  => 'invoice-company',
                'attribute'  => 'company',
                'value' => $model->company,
                'options' => ['multiple' => false],
                'pluginOptions' => array(
                    'placeholder' => Yii::t('app.invoice', 'Company'),
                    'allowClear' => true,
                    'tags' => true,
                    'minimumInputLength' => 1,
                    'ajax' => [
                            'url' => yii\helpers\Url::to(['/finance/invoice/suggest']),
                            'dataType' => 'json',
                    ],
                ),
                'pluginEvents' => [
                    "change" => "function(event){
                        var id = event.target.value;
                        // 获取联系人其它信息
                        if(id){
                            $.get('/finance/invoice/find-company', { id: id},
                                function(data){
                                    if(data.tax){
                                        $('#invoice-tax').val(data.tax);
                                    }
                                    if(data.bank_account){
                                        $('#invoice-bank_account').val(data.bank_account);
                                    }
                                    if(data.bank_name){
                                        $('#invoice-bank_name').val(data.bank_name);
                                    }
                                    if(data.address){
                                        $('#invoice-address').val(data.address);
                                    }
                                    if(data.phone){
                                        $('#invoice-phone').val(data.phone);
                                    }

                                    $('#invoice-company').find('option[value='+data.id+']').attr('value', data.company);
                            });
                        }
                    }",
                ],
        ]) ?>
        </div>
    </div>

    <?= $form->field($model, 'tax')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'bank_account')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'bank_name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'address')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'phone')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'remark')->textInput(['maxlength' => true]) ?>
    </div>
    <div class="col-md-6">
        <div class="col-md-12" style="border-bottom:1px solid #ddd;color: red;">
            <h4 style="line-height: 30px">订单金额
                <p class="pull-right">
                    <a class="btn btn-primary" id="setPrice">设置金额</a>
                </p>
            </h4>
        </div>
    <?php if($model->isNewRecord){ ?>
        <?php foreach($orders as $key=>$order){ ?>
           <div class="col-md-12" style="border-bottom: 1px solid #ddd;padding:5px;" >
                <div class="col-md-6">
                    <?=$order->order->code?> /  <?=$order->order->username?> / <?=$order->order->phone?>
                    <br> 
                    总金额： <?=$order->price?> 
                    <br>
                    已开发票金额：<?=$price_arr[$order->order_id]?>  
                </div>
                <input type="hidden" name="InvoiceOrder[<?=$key?>][order_id]" value="<?=$order->order->id?>">
                <div class="col-md-6">
                    <?php echo TouchSpin::widget([
                            'name' => "InvoiceOrder[$key][price]",
                            'id' =>'order-price-'.$order->order_id,
                            'options' => ['class'=>'price','data-max'=>$order->price-$price_arr[$order->order_id]],
                            'pluginOptions' => [
                                'min' => 0,
                                'max' => $order->price-$price_arr[$order->order_id],
                                'step' => 0.01,
                                'decimals' => 2,
                                'verticalbuttons' => true,
                                'verticalupclass' => '',
                                'verticaldownclass' => '',
                            ],
                        ]); ?>
                </div>
           </div> 
        <?php }?>   
    <?php }else{ ?>


        <?php foreach($orders as $key=>$order){ ?>
           <div class="col-md-12" style="border-bottom: 1px solid #ddd;padding:5px;" >
                <div class="col-md-6">
                    <?=$order->order->code?> /  <?=$order->order->username?> / <?=$order->order->phone?>
                    <br> 
                    总金额： <?=$order->price?> 
                    <br>
                    已开发票金额：<?=$price_arr[$order->order_id]?>  
                </div>
                <input type="hidden" name="InvoiceOrder[<?=$key?>][order_id]" value="<?=$order->order->id?>">
                <div class="col-md-6">
                    <?php echo TouchSpin::widget([
                            'name' => "InvoiceOrder[$key][price]",
                            'id' =>'order-price-'.$order->order_id,
                            'value' =>$II_pirce[$order->order_id],
                            'options' => ['class'=>'price','data-max'=>$order->price-$price_arr[$order->order_id]],
                            'pluginOptions' => [
                                'min' => 0,
                                'max' => $order->price-$price_arr[$order->order_id],
                                'step' => 0.01,
                                'decimals' => 2,
                                'verticalbuttons' => true,
                                'verticalupclass' => '',
                                'verticaldownclass' => '',
                            ],
                        ]); ?>
                </div>
           </div> 
        <?php }?>   


    
    <?php }?>    


    
    </div>
    <div class="col-md-12 text-center">
        <?= Html::submitButton($model->isNewRecord ? '添加发票' : '确认修改', ['class' =>  'col-md-2  col-md-offset-5 btn btn-success ']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
    // 清除模态框缓存
    $(".invoice-modal").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
        window.location.reload();
    });

    $("#setPrice").click(function(){
        $(".price").each(function(){
            var max =  $(this).data('max');
            $(this).val(max);
        });
    });
</script>
<?php app\components\JsBlock::end() ?>
